﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;

namespace BTM_Client_DBwork
{
    public interface IRepository<T> where T : class
    {
        void Add(T item, int id);

        void Remove(T item);

        T GetItem(string itemName);

        void SessionClose();
    }

    class DbRepository<T> : IRepository<T>, IDisposable where T : class, new()
    {
        private DBhelper _helper = new DBhelper();

        public void Add(T item, int id)
        {
            using (var session = _helper.Session)
            {
                if (id != -1)
                    session.Save(item, id);
                else
                    session.Save(item);
            }
        }

        public void Remove(T item)
        {
            using (var session = _helper.Session)
            {
                session.Delete(item);
            }
        }

        public T GetItem(string itemName)
        {
            throw new NotImplementedException();
        }

        public void SessionClose()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            SessionClose();
        }
    }
}
